package com.moore.service.Impl;

import com.moore.bean.Family;
import com.moore.bean.Tenant;
import com.moore.bean.TenantInfo;
import com.moore.dto.ResponseData;
import com.moore.mapper.TenantMapper;
import com.moore.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.moore.utils.Logger.toLog;

@Service
public class TenantServiceImpl implements TenantService {
    @Autowired
    private TenantMapper tenantMapper;
    @Override
    public ResponseData regTenant(Tenant tenant) {
        List<Tenant> tenantList = tenantMapper.queryTenant(tenant.getAccount(),"");
        if (!tenantList.isEmpty()){
            toLog("该账号已存在，请重新输入");
            new ResponseData(400,"该账号已存在，请重新输入",null,0);
        } else {
            int result = tenantMapper.registerTenant(tenant);
            if (result == 1) {
                toLog("注册成功");
                return new ResponseData(200, "注册成功", null, 0);
            } else {
                toLog("注册失败");
                new ResponseData(500, "注册失败", null, 0);
            }
        }
        return null;
    }

    @Override
    public ResponseData saveTenantInfo(TenantInfo tenantInfo, List<Family> familyList) {
        toLog("正在查询租客资料是否存在");
        List<TenantInfo> tenantInfoList = tenantMapper.queryTenantInfo(tenantInfo.getTenant());
        if (tenantInfoList.isEmpty()) {
            toLog("租客资料不存在，正在保存租客资料");
            int result = tenantMapper.saveTenantInfo(tenantInfo);
            if (result == 0) {
                toLog("租客资料保存失败");
                return new ResponseData(500, "用户资料保存失败", null, 0);
            }
        } else {
            toLog("租客资料已存在，正在修改租客资料");
            int result = tenantMapper.updateTenantInfo(tenantInfo);
            if (result == 0) {
                toLog("租客资料更新失败");
                return new ResponseData(500, "用户资料更新失败", null, 0);
            }
        }
        if (!familyList.isEmpty()){
            toLog("正在查询租客家庭成员资料是否存在");
            List<Family> families = tenantMapper.queryTenantFamily(tenantInfo.getTenant());
            if (families.isEmpty()) {
                toLog("租客家庭成员资料不存在，正在保存租客家庭成员资料");
                int result = tenantMapper.saveTenantFamily(familyList);
                if (result == 0) {
                    toLog("租客家庭成员保存失败");
                    return new ResponseData(500, "租客家庭成员保存失败", null, 0);
                }
            } else {
                toLog("租客家庭成员资料已存在，正在重新添加租客家庭成员资料");
                int result = tenantMapper.deleteTenantFamily(tenantInfo.getTenant(), "");
                if (result == 0) {
                    toLog("租客家庭成员删除失败");
                    return new ResponseData(500, "租客家庭成员删除失败", null, 0);
                }
                int result1 = tenantMapper.saveTenantFamily(familyList);
                if (result1 == 0) {
                    toLog("租客家庭成员保存失败");
                    return new ResponseData(500, "租客家庭成员保存失败", null, 0);
                }
            }
        }
        toLog("租客资料和家庭成员保存成功");
        return new ResponseData(200, "保存成功", null, 0);
    }

    @Override
    public ResponseData tenantLogin(Tenant tenant) {
        String account = tenant.getAccount();
        String password = tenant.getPassword();
        List<Tenant> tenantList = tenantMapper.queryTenant(account,password);
        if (tenantList.isEmpty()){
            toLog("账号或密码错误");
            return new ResponseData(500,"账号或密码错误",null,0);
        } else {
            toLog("登录成功");
            return new ResponseData(200,"登录成功",tenantList,0);
        }
    }

    @Override
    public ResponseData updateTenant(Tenant tenant) {
        int result = tenantMapper.updateTenant(tenant);
        if (result == 1) {
            toLog("修改成功");
            return new ResponseData(200, "修改成功", null, 0);
        } else {
            toLog("修改失败");
            return new ResponseData(500, "修改失败", null, 0);
        }
    }

    @Override
    public ResponseData queryTenantInfo(String tenant, HttpServletRequest request) {
        List<TenantInfo> tenantInfoList = tenantMapper.queryTenantInfo(tenant);
        String serverUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        for (TenantInfo value : tenantInfoList) {
            value.setIdUpload(serverUrl + value.getIdCardImage());
            value.setHukouUpload(serverUrl + value.getHouseholdRegister());
            value.setGraduationCertificate(serverUrl + value.getDiploma());
        }
        List<Family> tenantFamilyList = tenantMapper.queryTenantFamily(tenant);
        for (Family value : tenantFamilyList) {
            value.setIdUpload(serverUrl + value.getIdCardImage());
            value.setHukouUpload(serverUrl + value.getHouseholdRegister());
        }
        toLog("查租客资料和家庭信息成功");
        Map<String, Object> response = new HashMap<>();
        response.put("tenantInfoList", tenantInfoList);
        response.put("tenantFamilyList", tenantFamilyList);
        return new ResponseData(200,"查询成功",response,0);
    }

    @Override
    public ResponseData getTenantCountByMonth() {
        List<Map<String, Object>> tenantCountByMonth = tenantMapper.getTenantCountByMonth();
        toLog("查询租客数量成功");
        return new ResponseData(200,"查询成功",tenantCountByMonth,0);
    }

}
